/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView;
/**
 * 说明：
 * 销售退货汇总
 * @Author：李智深
 * @Date：2012-03-20
 */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

import com.ruipengkj.bean.TOrderReturn;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.TabelUtil;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.JPanelGridLayoutComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.RPJButtonRoundBlue;
import com.ruipengkj.component.RPJButtonRoundGray;
import com.ruipengkj.component.jpanel.QueryPanel;
import com.ruipengkj.component.page.PageDelegale;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.OrderReturnModel;
import com.ruipengkj.service.TOrderReturnService;
import com.ruipengkj.view.base.BasePagePanel;

public class MarketReturnedPanel extends BasePagePanel<TOrderReturn> implements PageDelegale ,Runnable{

	private static final long serialVersionUID = 1L;

	private JLabelJTextFieldComponent returnPeople = null;//退货人	
	
	private JLabelJTextFieldComponent returnID;//退货单编号
	
	private DataPackageComponent startTime; //操作时间【当前时间】
	private DataPackageComponent endTime; //操作时间【当前时间】
	
	private JButton queryButton;//查询
	private JButton clearButton;//清空
	private JButton showButton; //查看详细
	private JButton addButton; //添加退货单
	
	private JTable table;
	private JScrollPane tableJsp;
	private OrderReturnModel orderReturnModel;
	private PageView<TOrderReturn> pageView;//翻页主键
	private PagingComponent pagingComponent;//翻页组件
	private TOrderReturnService orderReturnService;//退货操作对象
	
	private MarketReturnedAddOrModifyDialog marketReturnedAddOrModifyDialog;//修改或添加面板
	
	public MarketReturnedPanel() {
		setLayout(new BorderLayout());
		this.init();
		this.initListener();
	}
	
	
	private void init() {
		orderReturnService=BuildSpringApplication.getInstance().getTOrderReturnServiceImpl();
		pageView = new PageView<TOrderReturn>();
		
		orderReturnModel = new OrderReturnModel();
		table=uiFactory.createRPJTable(orderReturnModel, null, this, 30, JLabel.CENTER);
		tableJsp=uiFactory.createJScrollPane(table);
		
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		QueryPanel queryPanel=new QueryPanel("销售退货",ImageFactory.loadImage(ImageFactory.FUN_1_ICON_5));
		FlowLayout flowLayout=new FlowLayout(FlowLayout.LEFT);
		JPanel panel = new JPanel(flowLayout);
		returnID = new JLabelJTextFieldComponent("退货单编号：", 20);
		returnPeople = new JLabelJTextFieldComponent("退 货 人：",20);
		startTime = new DataPackageComponent("开始时间：", 15);
		endTime = new DataPackageComponent("结束时间：", 15);
		queryButton = new RPJButtonBlue("查询");
		clearButton= new RPJButtonGray("清空");
		panel.add(returnID);
		panel.add(returnPeople);
		panel.add(startTime);
		panel.add(endTime);
		panel.add(queryButton);
		panel.add(clearButton);
		queryPanel.addPanel(panel);
//		queryPanel.addBoxPanel(panel);
		this.add(queryPanel, BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		this.add(downPanel(),BorderLayout.SOUTH);
	}
	
	private JPanel downPanel(){
		addButton = new RPJButtonRoundBlue("添加(Ctrl+A)");
		showButton= new RPJButtonRoundGray("查看详细");
		
		JPanel jp2 = new JPanelFlowLayoutComponent(new Component[]{new JLabel("   "),addButton,showButton}, FlowLayout.LEFT);
		JPanel tail_panel = new JPanelGridLayoutComponent(new Component[]{jp2,pagingComponent}, 1, 2);
		return tail_panel;
	}
	
	
	private void initListener(){
		clearButton.addActionListener(this);
		returnID.getText().addKeyListener(this);
		returnPeople.getText().addKeyListener(this);
		queryButton.addActionListener(this);
		showButton.addActionListener(this);
		addButton.addActionListener(this);
		addButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void run() {
		this.termQuery(1);
	}
	
	//条件查询
	protected void termQuery(final int currentPage){
		new Thread(){
			public void run(){
				final TOrderReturn orderReturn=new TOrderReturn();
				orderReturn.setReturnID(returnID.getText().getText().trim());
				TUser user=new TUser();
				user.setRealName(returnPeople.getText().getText().trim());
				orderReturn.setTUser(user);
				orderReturn.setStartTime(startTime.getDate());
				orderReturn.setEndTime(endTime.getDate());
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<TOrderReturn> pageModel = orderReturnService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),orderReturn);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					System.out.println(pageModel);
					if(pageModel!=null){
						orderReturnModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								TabelUtil.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						orderReturnModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}	
			}
		}.start();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==queryButton){
			//查询按钮
			this.termQuery(1);
		}else if(e.getSource()==addButton){
			//添加此单按钮
			marketReturnedAddOrModifyDialog=new MarketReturnedAddOrModifyDialog(null,"销售退货",true);
			marketReturnedAddOrModifyDialog.showDialog(null);
			if(marketReturnedAddOrModifyDialog.isSuccess) {
				this.termQuery(1);
			}
		}else if(e.getSource()==showButton){
			//查看详细
			if(table.getRowCount()!=0){
				int row=table.getSelectedRow();
				if(row!=-1){
					String orderReturnID=(String)table.getValueAt(row, 0);
					marketReturnedAddOrModifyDialog = new MarketReturnedAddOrModifyDialog(null,"销售退货详情",true);
					marketReturnedAddOrModifyDialog.showDialog(orderReturnID);
					if(marketReturnedAddOrModifyDialog.isSuccess){
						this.termQuery(1);
					}
				}else{
					JOptionPane.showMessageDialog(null,"请选择行再进行修改");
					return;
				}
			}
		}else if(e.getSource()==clearButton){
			//清空
			returnPeople.getText().setText("");//退货人	
			returnID.getText().setText("");//退货单编号
			startTime.setDate(null); //操作时间【当前时间】
			endTime.setDate(null); //操作时间【当前时间】
		}
	}


	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getSource()==returnID.getText()||e.getSource()==returnPeople.getText()){
			//查询
			if(e.getKeyCode()==KeyEvent.VK_ENTER){
				queryButton.doClick();
			}
		}
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getSource()==table){
			if(e.getClickCount() == 2){
				showButton.doClick();
    		}
		}
	}
	@Override
	protected PageView<TOrderReturn> getPageView() {
		return pageView;
	}


	@Override
	public void clear() {
		orderReturnModel.clear();
		returnPeople.getText().setText("");//退货人	
		returnID.getText().setText("");//退货单编号
		startTime.setDate(null);//操作时间【当前时间】
		endTime.setDate(null); //操作时间【当前时间】
	}
}
